package com.example.day03_2_consumer.controller;

import com.example.day03_2_consumer.feign.UserFeign;
import com.example.day03_2_consumer.pojo.User;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
@RequestMapping("/consumerUser")
@Slf4j
public class ConsumerUserController {

    @Autowired
    //注入feign接口的代理对象
    private UserFeign userFeign;

    @RequestMapping("{id}")
    @HystrixCommand
    public String findById(@PathVariable("id") Integer id) {
        /*if(id==1){
            throw new RuntimeException();
        }*/
        User user = userFeign.fbi(id);
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            String s = objectMapper.writeValueAsString(user);
            return s;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "";

    }



}
